package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 淮安日报
 * 
 * @author Chase  E-mail:594126573@qq.com
 *
 * @version 创建时间：2012-12-3  下午04:32:13
 * 
 */
public class HynewsNewsPaperAnalyse implements AnalyseNews {

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
						
		newspaper.setUrl(url);		
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
		
		if (url.startsWith("http://szb.hynews.net/")) {
			title =doc.select("td.font01").text();
			date=DateUtils.matchDate(url);
			content =HtmlCleaner.getContentHtml(url,doc.select("founder-content"));
		} else {
			title =doc.select("td.title_a").text();
			dateStr = doc.select("span.blue2").text();
			date=DateUtils.matchDate(dateStr);
			content =HtmlCleaner.getContentHtml(url,doc.select("founder-content"));
		}
	
		
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		/*
		 * http://szb.hynews.net/harb/html/2012-12/03/content_2482883.htm
		 */
		String[] regex = {
				 "http://szb.hynews.net/(hhwb|harb)/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_[0-9]*.htm"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		/*
		 * http://old.hynews.net/harb/html/2011-03/31/node_23.htm
		 */
		String[] regex = {
				"http://szb.hynews.net/(hhwb|harb)/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_[0-9]*.htm"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
}
